package com.crazy.web.service.share.impl;

import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crazy.model.major.entity.TLabel;
import com.crazy.model.major.entity.TLabelArticleRelation;
import com.crazy.model.major.service.TLabelArticleRelationService;
import com.crazy.model.major.service.TLabelService;
import com.crazy.web.service.share.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhaochaofeng
 * @version 1.0
 * @date 2020/11/26 11:27
 */
@Service
public class LabelServiceImpl implements LabelService {
    @Autowired
    private TLabelService tLabelService;
    @Autowired
    private TLabelArticleRelationService tLabelArticleRelationService;

    @Override
    public IPage<TLabel> find(long userId, long page, long rows, String search) {
        QueryWrapper<TLabel> queryWrapper1 = new QueryWrapper<>();
        if(StrUtil.isNotEmpty(search)){
            queryWrapper1.eq(TLabel.Columns.valid, 1);
            queryWrapper1.eq(TLabel.Columns.userId, userId);
            queryWrapper1.like(TLabel.Columns.labelName, search);
            QueryWrapper<TLabel> queryWrapper2 = queryWrapper1.or();
            queryWrapper2.eq(TLabel.Columns.valid, 1);
            queryWrapper2.eq(TLabel.Columns.userId, userId);
            queryWrapper2.like(TLabel.Columns.alias, search);
        }else{
            queryWrapper1.eq(TLabel.Columns.userId, userId);
            queryWrapper1.eq(TLabel.Columns.valid, 1);
        }
        queryWrapper1.orderByDesc(TLabel.Columns.id);
        Page<TLabel> labelPage = new Page<TLabel>(page, rows);
        IPage<TLabel> labelIPage = tLabelService.page(labelPage, queryWrapper1);
        return labelIPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(long userId, TLabel tLabel) {
        tLabel.setUserId(userId);
        tLabel.setCreateTime(LocalDateTime.now());
        tLabel.setUpdateTime(LocalDateTime.now());
        if(!tLabelService.save(tLabel)){
            //失败
            throw new RuntimeException("插入失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(long userId, long[] ids) {
        for(long id : ids){
            TLabel tLabel = tLabelService.getById(id);
            if(tLabel.getUserId()==null || userId!=tLabel.getUserId().longValue()){
                throw new ValidateException("无权删除 "+tLabel.getLabelName()+" 标签");
            }
            tLabel.setValid(0);
            tLabelService.updateById(tLabel);
        }
    }

    @Override
    public List<String> findAllByUserId(long userId) {
        QueryWrapper<TLabel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(TLabel.Columns.valid, 1);
        queryWrapper.eq(TLabel.Columns.userId, userId);
        List<TLabel> labels = tLabelService.list(queryWrapper);
        return labels.stream().filter(c -> c!=null).map(c -> c.getLabelName()).collect(Collectors.toList());
    }

    @Override
    public List<TLabel> findByArticleId(long articleId) {
        QueryWrapper<TLabelArticleRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(TLabelArticleRelation.Columns.articleId, articleId);
        List<TLabelArticleRelation> labelArticleRelations = tLabelArticleRelationService.list(queryWrapper);
        List<TLabel> labelList = new ArrayList<>();
        for(TLabelArticleRelation labelArticleRelation : labelArticleRelations){
            TLabel label = tLabelService.getById(labelArticleRelation.getLabelId());
            labelList.add(label);
        }
        return labelList;
    }
}
